//
//  NewFeatureViewController.swift
//  WeChat
//
//  Created by 梁迪 on 15/12/16.
//  Copyright © 2015年 梁迪. All rights reserved.
//

import UIKit
import SnapKit

private let imageCount = 4
private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

 

//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return imageCount
//    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
    
      let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        
        cell.index = indexPath.item
        
        
    
        return cell
    }

    
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        let cell = collectionView.visibleCells().last! as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)!
    
        if index.item == imageCount - 1 {
            cell.showAnimation()
            
        }
        
    }

}









class NewFeatureCell: UICollectionViewCell {
    
    var index: Int = 0{
        didSet{
            
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            
            startButton.hidden = true
            
        }
      
        
    }
    
    
    private func showAnimation(){
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.hidden = false
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
           self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("OK")
        }
        
        
    }
    
    
    // 重写构造方法, cell 构造的入口
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
    // 定义的子视图,需要添加在contentView 上
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
            
        }
startButton.snp_makeConstraints { (make) -> Void in
    
    make.centerX.equalTo(contentView.snp_centerX)
    make.bottom.equalTo(contentView.snp_bottom).offset(-180)
    
    
        }
    
    startButton.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    
    
    }
    
    @objc private func startBtnDidClick(){
        
       NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
        
    }
    
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startButton :UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        
       return btn
    }()
    
}





